package cn.dujc.superkid.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.ResultAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.AnswerListResult;
import cn.dujc.superkid.bean.CourseEntity;
import cn.dujc.superkid.bean.CourseResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.configs.Globals;
import cn.dujc.superkid.configs.ICallBack;
import cn.dujc.superkid.configs.SearchView;
import cn.dujc.superkid.configs.bCallBack;
import cn.dujc.superkid.utils.Preferences;

public class SearchResultActivity extends BaseActivity {
    public static final String EXTRA_COURSE = "EXTRA_COURSE";
    private LinearLayout ll_history;
    private SearchView searchView;
    private cn.dujc.superkid.configs.EditText_Clear et_search;
    private String content;
    private String searchContent;
    private RecyclerView recyclerView;
    private Button search_back;
    private ResultAdapter resultAdapter;
    private List<CourseEntity> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        searchContent = extras().get(EXTRA_COURSE);
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search);
        if (searchContent==null){
            Intent intent = getIntent();
            searchContent = intent.getStringExtra("searchContent");
        }
        initViews();
        initLogic();
        loadData();
    }

    private void initLogic() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, linearLayoutManager.getOrientation()));
        resultAdapter = new ResultAdapter(list);
        recyclerView.setAdapter(resultAdapter);

    }

    private void initViews() {
        ll_history=findViewById(R.id.ll_history);
        ll_history.setVisibility(View.GONE);
        search_back = findViewById(R.id.search_back);
        recyclerView = findViewById(R.id.result_recycler);
        searchView = (SearchView) findViewById(R.id.search_view);
        et_search = findViewById(R.id.et_search);
        et_search.setText(searchContent);
        content=searchContent;//
        // 4. 设置点击搜索按键后的操作（通过回调接口）
        // 参数 = 搜索框输入的内容
        searchView.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String string) {
                content=et_search.getText().toString();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (content.isEmpty()){
                    ToastUtil.showToast(mActivity,"请输入课程关键字");
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                }else {
                    loadData();
                   // 隐藏软键盘
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

                }
            }
        });
        // 5. 设置点击返回按键后的操作（通过回调接口）
        searchView.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });

    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }else{
            HttpHelper.getHome().search(QMap.build()
                    .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                    .put("keywords", content)
                    .create()).enqueue(new ResultCallBack<Result<CourseResult>>(mActivity) {
                @Override
                public void onSuccessCallback(int code, Result<CourseResult> body) {
                    list.clear();
                    if (body != null){
                        final CourseResult data = body.getData();
                        if (data != null) {
                            final List<CourseEntity> list = data.getList();
                            if (list != null) {
                                SearchResultActivity.this.list.addAll(list);
                            }
                        }
                    }
                    resultAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailureCallback(int code, String msg) {
                    super.onFailureCallback(code, msg);
                    list.clear();
                    resultAdapter.notifyDataSetChanged();
                }
            });
        }

    }

    @Override
    public int getViewId() {
        return 0;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {

    }
}
